<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/static/lib/PearAdmin/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <!--查询框-->
    <div class="layui-card-body">
        <form class="layui-form" action="javascript:void(0);">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="nickName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!--数据表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<!-- 数据表格工具栏 -->
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        禁用
    </button>
</script>

<!-- 数据表格工具条 -->
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
</script>

<!-- 数据表格启用列模板 -->
<script type="text/html" id="user-enable">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{ d.status!= '0' ? 'checked' : '' }} />
</script>
<!-- 数据表格锁定列模板 -->
<script type="text/html" id="user-lock">
    #[[
        {{# if(d.status == '2'){ }}
        <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|锁定" lay-filter="user-unlock" />
        {{# } }}
    ]]#
</script>

<script src="/static/lib/PearAdmin/component/layui/layui.js"></script>
<script src="/static/lib/PearAdmin/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery','common', 'popup'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let popup = layui.popup;

        //数据表格的列
        let cols = [
            [
                {type: 'checkbox'},
                {title: '账号', field: 'userName', align: 'center', width: 100},
                {title: '姓名', field: 'nickName', align: 'center' },
                {title: '最后登录', field: 'loginTime', align: 'center', width: 180 },
                {title: '状态', field: 'enable', align: 'center', templet: '#user-enable', width: 120 },
                {title: '锁定', field: 'lock', align: 'center', templet: '#user-lock', width: 100 },
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 130 }
            ]
        ]

        //渲染数据表格
        table.render({
            elem: '#user-table',
            url: '/account/getAccounts',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        //监听工具条事件
        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        //监听工具栏事件
        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        //监听解锁按钮事件
        form.on('switch(user-unlock)', function(obj) {
            $.post("/account/unlock", {id:this.value}, function(data){
                if("1"==data.state){
                    popup.success(data.msg);
                    table.reload('user-table',{})
                } else {
                    popup.failure(data.msg);
                }
            });
        });

        //查询
        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        //监听用户状态开关
        form.on('switch(user-enable)', function(obj) {
            $.post("/account/updateStruts", {id:this.value, status:obj.elem.checked}, function(data){
                if("1"==data.state){
                    popup.success(data.msg);
                } else {
                    popup.failure(data.msg);
                }
            });
        });

        //新增按钮事件
        window.add = function() {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: [common.isModile()?'100%':'500px', common.isModile()?'100%':'450px'],
                content: '/account/enterAccountAdd',
                end: function(){ //弹窗关闭后重新加载数据
                    table.reload('user-table',{})
                }
            });
        }

        //编辑按钮事件
        window.edit = function(obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '450px'],
                content: '/account/enterAccountEdit/' + obj.data['id'],
                end: function(){ //弹窗关闭后重新加载数据
                    table.reload('user-table',{})
                }
            });
        }

        //批量删除/禁用
        window.batchRemove = function(obj) {
            let checkIds = common.checkField(obj,'id');

            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要禁用这些用户？', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/account/batchRemove/" + checkIds,
                    dataType: 'json',
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if ("1"==result.state) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
